package com.csdj.t309.geamtest.util;

import com.sun.mail.imap.protocol.Item;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.TreeMap;

import static com.csdj.t309.geamtest.service.Hashs.byteToString;

public class CheckSign {
    public static boolean checkSign(TreeMap<String,String> map,String hash){
        try{
            String secretKey = "wienq31fj2ijsf21f2g3";
            StringBuilder sa = new StringBuilder();
            map.entrySet().forEach(Item -> {
                sa.append(Item.getKey()).append("=").append(Item.getValue()).append("&");
            });
            String data = sa.substring(0,sa.length()-2);
            sa.append("secretKey=").append(secretKey);
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(data.getBytes("UTF-8"));
            byte[] byhash1 = messageDigest.digest();
            String aahash = byteToString(byhash1);
            return aahash.equals(hash);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return false;
    }
}
